//! 用户模块
const express = require('express');
//!引入数据库连接池
const pool = require('../pool');
//!  1.创建路由器
const router = express.Router();
//!  2.响应
//! 通过bid查询菜谱相关信息
router.get('/',(req,res)=>{
    let obj = req.query;
    console.log(obj);
    let sql = 'select b.bid,b.uid,rate,title,`describe`,pic,ingredient,content,cid,create_time,u.uname ,u.upic from cook_books AS b INNER JOIN cook_users as u ON b.uid=u.uid where bid=?'
    pool.query(sql, [obj.bid],(err,result)=>{
        console.log(result);
        if(err) throw err;
        if(result.length==0){
            res.send({code:200,msg:'数据不存在'});
        }else{
            res.send({code:200,msg:'查询成功',data:result});
        }
    });
});

//!上传
router.post('/update',(req,res,next)=>{
    //!得到数据对象body
    let obj = req.body;
    console.log(obj);
    //!执行sql语句
    pool.query('insert into cook_books set ?',[obj],(err,result)=>{
        if(err) {
            //!处理错误
            next(err);
            return;
        }
        res.send({code:200,msg:'上传成功'});
        console.log(obj);
    });
});


//!获得类别
router.get('/category',(req,res,next)=>{
    //!执行sql语句
    pool.query('select cid,cname from cook_category ',(err,result)=>{
        if(err) {
            //!处理错误
            next(err);
            return;
        }
        res.send({code:200,msg:'查询成功',data:result});
    });
});

//!获得类别对应的菜谱
router.get('/categorylist',(req,res,next)=>{
    let cid = req.query.cid;
    console.log(cid);
    //!执行sql语句
    pool.query('select bid,title,pic,`describe`,cid from cook_books where cid=?',[cid],(err,result)=>{
        if(err) {
            //!处理错误
            next(err);
            return;
        }
        res.send({code:200,msg:'查询成功',data:result});
        // console.log(resul);
    });
});
//! 3.导出路由器
module.exports = router;







